/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.beepmystuff;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * Implements an activity for entering product codes by hand.
 */
public class ManualEntryActivity extends Activity {
  /**
   * Array of ids associated with the button grid.
   * @see #KEYCODES
   */
  private static final int[] BUTTONS = {
    R.id.zero, R.id.one, R.id.two, R.id.three, R.id.four,
    R.id.five, R.id.six, R.id.seven, R.id.eight, R.id.nine,
    R.id.backspace
  };
  /**
   * Array of key codes corresponding to pressing the on-screen buttons.
   * Must be in the same order as the BUTTONS array.
   * @see #BUTTONS
   */
  private static final int[] KEYCODES = {
    KeyEvent.KEYCODE_0, KeyEvent.KEYCODE_1, KeyEvent.KEYCODE_2, KeyEvent.KEYCODE_3,
    KeyEvent.KEYCODE_4, KeyEvent.KEYCODE_5, KeyEvent.KEYCODE_6, KeyEvent.KEYCODE_7,
    KeyEvent.KEYCODE_8, KeyEvent.KEYCODE_9, KeyEvent.KEYCODE_DEL,
  };

  private EditText editText;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.manual_entry);

    editText = (EditText) findViewById(R.id.manual_entry_edit);
    Button submitButton = (Button) findViewById(R.id.submit);
    submitButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        submit();
      }
    });

    OnClickListener buttonListener = new OnClickListener() {
      @Override
      public void onClick(View view) {
        for (int i = 0; i < BUTTONS.length; ++i) {
          if (BUTTONS[i] == view.getId()) {
            handleButtonPress(i);
            return;
          }
        }
        throw new AssertionError("Unknown button clicked");
      }
    };
    for (int buttonId : BUTTONS) {
      Button button = (Button) findViewById(buttonId);
      button.setOnClickListener(buttonListener);
    }
  }

  public void submit() {
    Intent result = new Intent();
    result.putExtra("SCAN_RESULT", editText.getText().toString());
    result.putExtra("SCAN_TYPE", "MANUAL");
    setResult(RESULT_OK, result);
    finish();
  }

  public void handleButtonPress(int buttonIndex) {
    int keyCode = KEYCODES[buttonIndex];
    editText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
    editText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyCode));
  }
}
